package com.papper.common.config;

import com.google.common.collect.Maps;
import com.papper.common.bean.sys.SysCode;
import com.papper.common.service.sys.SysCodeService;
import com.papper.common.utils.PapperSpringUtil;
import com.papper.common.utils.tool.StringTool;

import java.util.List;
import java.util.Map;

/**
 * @Author:Lajiao
 * @Date:2014年5月20日
 * @Time:下午5:58:45
 * @Description:系统字典信息
 */
public class SysCodeHelp {
    private static       SysCodeHelp         instance           = null;
    private final static String              DEFAULT_NULL_VALUE = "NULL";
    private static       Map<String, String> Config             = Maps.newHashMap();

    private SysCodeHelp() {
    }

    public static synchronized SysCodeHelp I() {
        if (instance == null) {
            instance = new SysCodeHelp();
            instance.initPropertys();
        }
        return instance;
    }

    public void initPropertys() {
        Config.clear();
        try {
            // 系统字典
            SysCodeService s           = (SysCodeService) PapperSpringUtil.getBean("sysCodeService");
            List<SysCode>  lstProperty = s.findAll();
            if (lstProperty != null && !lstProperty.isEmpty()) {
                Map<Long, String> topKey = Maps.newHashMap();
                for (SysCode p : lstProperty) {
                    topKey.put(p.getId(), p.getCodeKey());
                }
                String key   = null;
                String value = null;
                for (SysCode p : lstProperty) {
                    if (p.getParentId().intValue() == 0) {
                        continue;
                    }
                    key = StringTool.nullToEmpty(topKey.get(p.getParentId().longValue())) + "." + p.getCodeKey();
                    value = p.getCodeValue();
                    Config.put(key, value);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String TGS(String tk, String k, String defaultValue) {
        String rtn = Config.get(tk + "." + k);
        if (rtn != null) {
            if (DEFAULT_NULL_VALUE.equals(rtn)) {
                return defaultValue;
            }
            return rtn;
        }
        return defaultValue;
    }

    public Double TGD(String tk, String k, Double defaultValue) {
        String rtn = Config.get(tk + "." + k);
        if (rtn != null) {
            if (DEFAULT_NULL_VALUE.equals(rtn)) {
                return null;
            }
            return StringTool.parseDouble(rtn, defaultValue);
        }
        return StringTool.parseDouble(rtn, defaultValue);
    }

    public Integer TGI(String tk, String k, Integer defaultValue) {
        String rtn = Config.get(tk + "." + k);
        if (rtn != null) {
            if (DEFAULT_NULL_VALUE.equals(rtn)) {
                return null;
            }
            return StringTool.parseInteger(rtn, defaultValue);
        }
        return StringTool.parseInteger(rtn, defaultValue);
    }

    public String GS(String key) {
        String rtn = Config.get("System." + key);
        if (rtn != null) {
            if (DEFAULT_NULL_VALUE.equals(rtn)) {
                return null;
            }
            return rtn;
        }
        return rtn;
    }

    public String GS(String key, String def) {
        String rtn = Config.get("System." + key);
        if (rtn != null) {
            if (DEFAULT_NULL_VALUE.equals(rtn)) {
                return def;
            }
            return rtn;
        }
        return def;
    }

    public int GI(String key) {
        return StringTool.parseInteger(GS(key), null);
    }

    public int GI(String key, int def) {
        return StringTool.parseInteger(GS(key), def);
    }

    public double GD(String key, double def) {
        return StringTool.parseDouble(GS(key), def);
    }
}
